import {reqAddOrUpdataCart, reqGoodsInfo} from "../../api";
import {getUUID} from "../../utils/uuid";

export default ({
    namespaced: true,
    state: {
        goodInfo: {},
        // 游客临时身份
        uuid: getUUID()
    },
    mutations: {
        GETGOODINFO(state, value) {
            state.goodInfo = value
        }
    },
    actions: {
        async getGoodInfo({commit}, params) {
            let result = await reqGoodsInfo(params)
            if (result.code == 200) {
                commit('GETGOODINFO', result.data)
            }
        },
        //加购请求
        async addOrUpdateShopCart({commit}, {skuId, skuNum}) {
            let result = await reqAddOrUpdataCart(skuId, skuNum)
            if (result.code == 200) {
                return 'success!'
            } else {
                return Promise.reject(new Error('faile'))
            }
        }
    },

    getters: {
        categoryView(state) {
            return state.goodInfo.categoryView || {};
        },
        skuInfo(state) {
            return state.goodInfo.skuInfo || {};
        },
        // skuImageList(state) {
        //     return state.goodInfo.skuInfo.skuDefaultImg || undefined;
        // }
    }

})